﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports System.Data.Common

Public Class Users
#Region "Constants"
    Protected Const _UserID As String = "UserID"
    Protected Const _UserName As String = "UserName"
    Protected Const _Password As String = "Password"
    Protected Const _AccountLocked As String = "AccountLocked"
    Protected Const _CreatedDate As String = "CreateDate"
#End Region

#Region "Private"
    Private Shared intUserID As Integer = 0
    Private Shared strUserName As String = String.Empty
    Private Shared strPassword As String = String.Empty
    Private Shared blAccountLocked As Boolean = False
    Private Shared dtCreateDate As Date
    Private Shared _blExist As Boolean
    Private Shared el As New errorLog
#End Region

#Region "Public"
    Public Property UserID() As Integer
        Get
            Return intUserID
        End Get
        Set(ByVal value As Integer)
            intUserID = value
        End Set
    End Property
    Public Property UserName() As String
        Get
            Return strUserName
        End Get
        Set(ByVal value As String)
            strUserName = value
        End Set
    End Property
    Public Property Password() As String
        Get
            Return strPassword
        End Get
        Set(ByVal value As String)
            strPassword = value
        End Set
    End Property

    Public Property AccountLocked() As Boolean
        Get
            Return blAccountLocked
        End Get
        Set(ByVal value As Boolean)
            blAccountLocked = value
        End Set
    End Property
    Public Property CreateDate() As String
        Get
            Return dtCreateDate
        End Get
        Set(ByVal value As String)
            dtCreateDate = value
        End Set
    End Property
#End Region

    Public Function LoadUserByUserName() As Boolean
        If Not (strUserName = String.Empty) Then
            _blExist = globalClass.Exist("Users_NameExist", strUserName, "@eUsername")
        End If

        If (_blExist) Then
            Dim db As Database = DbStoreFactory.CreateDatabase
            Dim cmd As DbCommand = DirectCast(Nothing, DbCommand)
            Dim reader As IDataReader = Nothing

            Try
                cmd = db.GetStoredProcCommand("GetUserByUserName")
                db.DiscoverParameters(cmd)
                cmd.Parameters("@strUserName").Value = strUserName

                reader = db.ExecuteReader(cmd)

                Do While (reader.Read)
                    intUserID = Convert.ToInt16(reader(_UserID))
                    strUserName = reader(_UserName).ToString
                    strPassword = reader(_Password).ToString
                    blAccountLocked = Convert.ToBoolean(reader(_AccountLocked))
                    dtCreateDate = Convert.ToDateTime(reader(_CreatedDate))
                Loop

            Catch ex As Exception
                el.CatchError(ex)
            Finally
                If Not (cmd Is Nothing) Then
                    cmd.Dispose()
                End If
                If Not (reader Is Nothing) Then
                    reader.Close()
                End If
            End Try
        End If

        Return _blExist
    End Function

End Class
